package com.google.code.swingonfire.component.clock;

import java.util.HashMap;
import java.util.Map;

/**
 * Describes the current state of the clock. Each of the hands is described in its own {@link JClockHandState}.
 * 
 * @author thred
 */
public class JClockState
{

    private final Map<JClockHandType, JClockHandState> states = new HashMap<JClockHandType, JClockHandState>();

    public JClockState()
    {
        super();

        reset();
    }

    /**
     * Resets the clock
     */
    public void reset()
    {
        states.clear();

        for (JClockHandType type : JClockHandType.values())
        {
            states.put(type, new JClockHandState(type));
        }
    }

    /**
     * Returns the state of the specified hand
     * 
     * @param type the hand
     * @return the state
     */
    public JClockHandState get(JClockHandType type)
    {
        return states.get(type);
    }

    /**
     * Sets the value of the specified hand
     * 
     * @param type the type of hand
     * @param value the value
     * @param animate true to animate the transition to this value
     * @return true, if the value has really changed
     */
    public boolean set(JClockHandType type, double value, boolean animate)
    {
        return get(type).set(value, animate);
    }

    public boolean set(JClockHandType type, double value, int rotation, boolean animate)
    {
        return get(type).set(value, rotation, animate);
    }

    public boolean update(double delay)
    {
        boolean updated = false;

        for (JClockHandState value : states.values())
        {
            updated |= value.update(delay);
        }

        return updated;
    }

    public JClockHandType getSelected()
    {
        for (JClockHandType type : JClockHandType.values())
        {
            if (get(type).isSelected())
            {
                return type;
            }
        }

        return null;
    }

    public boolean setSelected(JClockHandType selected)
    {
        JClockHandType currentlySelected = getSelected();

        if (currentlySelected == selected)
        {
            return false;
        }

        for (JClockHandType type : JClockHandType.values())
        {
            get(type).setSelected(selected == type);
        }

        return true;
    }
}
